﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

using Elegant.Ui;

namespace iPdf
{

    class EncryptPage: SecurityPage<EncryptOption>
    {
        #region Properties
        public override bool ReadyToAct
        {
            get
            {
                bool ready =  (OwnerPassword.Enabled && !string.IsNullOrEmpty(OwnerPassword.Text) &&
                            OwnerPassword.Text == AckOwnerPassword.Text )    ||
                            (Certificate.Enabled && Certificate.IsValid && 
                            (!Certificate.IsFile || (CertificatePassword.Enabled == true && !string.IsNullOrEmpty(CertificatePassword.Text)))
                            );
                return ready && base.ReadyToAct;
            }
        }

        protected override EncryptOption PageOption
        {
            get
            {
                EncryptOption option = new EncryptOption();

                //set security level
                SecurityLevel securityLevel = (SecurityLevel)EncryptMethod.SelectedItem;
                option.SecurityLevelId = securityLevel.Id;

                //set certificate
                if (Certificate.DefaultCertificate == null || !Certificate.DefaultCertificate.IsValid)
                {
                    throw new InvalidCertificateFile();
                }

                option.Certificate = Certificate.DefaultCertificate;
                option.Certificate.Password = CertificatePassword.Text;
                
                //set passwords
                option.OwnerPassword = OwnerPassword.Text;
                option.OpenPassword = OpenPassword.Text;

                //set permissions
                option.Permission = this.Permission;

                return option;
            }
        }

        public override string IntroInfo
        {
            get
            {
                return global::iPdf.Properties.Resources.EncryptPageIntroInfo;
            }
        }

#endregion

        public EncryptPage()
            : base()
        {
            //setup handlers for AckOwnerPassword
            this.AckOwnerPassword.TextChanged += new EventHandler(TextBox_TextChanged);

            actor = new PdfEncryptor();
        }

        public override void UpdatePageOnlyControls()
        {
            base.UpdatePageOnlyControls();

            if (Global.MainWindow.Busy || !Global.Grid.HasItems)
            {
                return;
            }

            SecurityLevel level = (SecurityLevel)EncryptMethod.SelectedItem;
            bool isPasswordSecurity = level.Id == SecurityLevelId.PasswordSecurity;
            this.AckOwnerPassword.Enabled = isPasswordSecurity;
            this.OpenPassword.Enabled = isPasswordSecurity;
            Certificate.Enabled = !isPasswordSecurity;

            Helper.EnableControl( PermissionsGroup, ReadyToAct );
            ScreenReaderPermission.Enabled = !CopyContentPermission.Checked;
        }

        public override void ImportSession(System.Xml.XmlElement sessionNode)
        {
            base.ImportSession(sessionNode);

            AckOwnerPassword.Text = OwnerPassword.Text;
            OpenPassword.Text = sessionNode.GetAttribute(OpenPassword.Name);
        }

        public override void ExportSession(System.Xml.XmlElement sessionNode)
        {
            base.ExportSession(sessionNode);

            SetXmlElementAttribute(sessionNode, OpenPassword.Name, OpenPassword.Text);
        }

        protected override void InitializeComponent()
        {
            base.InitializeComponent();
            this.AckOwnerPassword = new iPdf.HoverSelectionTextBox();
            this.OpenPassword = new iPdf.HoverSelectionTextBox();
 

            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();


            this.CryptoGroup.Controls.Add(this.AckOwnerPassword);
            this.CryptoGroup.Controls.Add(this.OpenPassword);
            // 
            // AckOwnerPassword
            // 
            this.AckOwnerPassword.Id = "ed95d828-eddc-4f91-8eeb-37d4da419573";
            this.AckOwnerPassword.ImeMode = System.Windows.Forms.ImeMode.Disable;
            this.AckOwnerPassword.InformativenessMinimumLevel = "Elegant.Ui.TextBoxInformativenessLevel:TextBoxWithLabel";
            this.AckOwnerPassword.LabelAreaWidthTemplate = "Owner Password";
            this.AckOwnerPassword.LabelText = "Ack Password";
            this.AckOwnerPassword.Location = new System.Drawing.Point(4, 50);
            this.AckOwnerPassword.Name = "AckOwnerPassword";
            this.AckOwnerPassword.PasswordChar = '●';
            this.AckOwnerPassword.Size = new System.Drawing.Size(211, 24);
            this.AckOwnerPassword.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Owner16)});
            this.AckOwnerPassword.TabIndex = 2;
            this.AckOwnerPassword.TextEditorWidth = 95;
            this.AckOwnerPassword.UseSystemPasswordChar = true;
            // 
            // OpenPassword
            // 
            this.OpenPassword.Id = "ba5837b0-98a7-4070-aadb-c09053b24fba";
            this.OpenPassword.ImeMode = System.Windows.Forms.ImeMode.Disable;
            this.OpenPassword.InformativenessMinimumLevel = "Elegant.Ui.TextBoxInformativenessLevel:TextBoxWithLabel";
            this.OpenPassword.LabelAreaWidthTemplate = "Owner Password";
            this.OpenPassword.LabelText = "Open Password";
            this.OpenPassword.Location = new System.Drawing.Point(217, 2);
            this.OpenPassword.Name = "OpenPassword";
            this.OpenPassword.PasswordChar = '●';
            this.OpenPassword.Size = new System.Drawing.Size(211, 24);
            this.OpenPassword.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.User16)});
            this.OpenPassword.TabIndex = 3;
            this.OpenPassword.TextEditorWidth = 95;
            this.OpenPassword.UseSystemPasswordChar = true;


            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        #region Fields
        private Elegant.Ui.TextBox AckOwnerPassword;
        private Elegant.Ui.TextBox OpenPassword;
        #endregion

    }
}
